//
//  XHBuyMoneyViewController.m
//  umbs
//
//  Created by 冷山 on 16/6/16.
//  Copyright © 2016年 冷山. All rights reserved.
//

#import "XHBuyMoneyViewController.h"
#import "XHGoodsViewController.h"
#import "XHMethodPaymentViewController.h"
#import "XHPaySuccessViewController.h"
#import "XHPayFailureViewController.h"

#import "XHMoneyBuyCell.h"
#import "XHEarningsCell.h"

#import "Services.h"
#import "APIKey.h"

#import <Pingpp/Pingpp.h>
#import <UIAlertView-Blocks/UIAlertView+Blocks.h>

@interface XHBuyMoneyViewController ()<XHMethodPaymentViewDelegate,XHGoodsViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (weak, nonatomic) IBOutlet UIButton *ui_buyNow;
@property (strong, nonatomic) NSDictionary *m_dicTable;

@end

@implementation XHBuyMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ui_buyNow.layer.masksToBounds = YES;
    self.ui_buyNow.layer.cornerRadius = 8;
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHMoneyBuyCell" bundle:nil] forCellReuseIdentifier:XHMoneyBuyCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHEarningsCell" bundle:nil] forCellReuseIdentifier:XHEarningsCellIdentifier];
    [self getAmount];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getDictionaryList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buyReloadData) name:@"BuyReloadData" object:nil];
}

- (void)buyReloadData
{
    [self.ui_tableView reloadData];
}

- (void)getAmount
{
    //判断是待支付订单 or 理财流量购买
    if ([self.m_myCartArray isKindOfClass:[NSArray class]])
    {
        float ogPrice;
        ogPrice = self.marketPrice - [self.m_totalMoney doubleValue];
        [self.ui_buyNow setTitle:[NSString stringWithFormat:@"立即支付%@元(已节省￥%.2f元)",self.m_totalMoney,ogPrice] forState:UIControlStateNormal];
    }
    else
    {
        double allPrice=0;
        NSArray * array = self.toPayDic[@"productList"];
        for (NSDictionary * dic in array)
        {
            double prices=[dic[@"marketPrice"] doubleValue];
            int count=[dic[@"originalCount"] intValue];
            allPrice+=(prices*count);
        }
        //        NSLog(@"%f",allPrice);
        double amount = [self.toPayDic[@"amount"] doubleValue];
        double ogPrice = allPrice-amount;
        [self.ui_buyNow setTitle:[NSString stringWithFormat:@"立即支付%.2f元(已节省￥%.2f)",amount,ogPrice] forState:UIControlStateNormal];
    }
}

//获取年化收益率
- (void)getDictionaryList
{
    [ServiceManager getDictionaryListWithSuccess:^(NSDictionary *data) {
        self.m_dicTable = data;
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [self.ui_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    } failure:^(NSDictionary *data) {
        
    }];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger number = 2;
    return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows = 1;
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = nil;
    if (indexPath.section==0)
    {
        cell = [self earningsCell];
    }
    else if (indexPath.section==1)
    {
        cell = [self moneyBuyCell:indexPath];
    }
    
    return cell;
}

- (XHEarningsCell*)earningsCell
{
    XHEarningsCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHEarningsCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CGFloat ratio = [self.m_dicTable[@"dictDataValue"] floatValue] * 100;
    NSString * str = [NSString stringWithFormat:@"%.f",ratio/15*100];
    CGFloat value = [str floatValue];
    [cell setRatio:value];
    [cell setNumber:[NSString stringWithFormat:@"%.1f",ratio]];
    NSLog(@"%lf,%lf",ratio,value);
    
    return cell;
}

- (XHMoneyBuyCell*)moneyBuyCell:(NSIndexPath*)indexPath
{
    XHMoneyBuyCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHMoneyBuyCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if([self.m_myCartArray isKindOfClass:[NSArray class]])
    {
        cell.ui_goodsCount.text = [NSString stringWithFormat:@"查看%ld件商品",self.m_myCartArray.count];
        if (self.m_myCartArray.count==0) {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage1.hidden = YES;
            cell.ui_trafficPackage2.hidden = YES;
            cell.ui_trafficPackage3.hidden = YES;
        }
        if (self.m_myCartArray.count>=1)
        {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage2.hidden = YES;
            cell.ui_trafficPackage3.hidden = YES;
            NSDictionary * dic = self.m_myCartArray[0];
            NSString * sequenceNBR = dic[@"sequenceNBR"];
            NSInteger number = [[self.m_myCartDictionary objectForKey:sequenceNBR] integerValue];
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_trafficPackage1.text = [NSString stringWithFormat:@"%@流量%ldM*%ld",dic[@"operatorName"],flowCount,number];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_trafficPackage1.text =[NSString stringWithFormat:@"%@流量%@G*%ld",dic[@"operatorName"],[formatter stringFromNumber:@(gcount)],number];
            }
        }
        if (self.m_myCartArray.count>=2)
        {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage2.hidden = NO;
            cell.ui_trafficPackage3.hidden = YES;
            NSDictionary * dic = self.m_myCartArray[1];
            NSString * sequenceNBR = dic[@"sequenceNBR"];
            NSInteger number = [[self.m_myCartDictionary objectForKey:sequenceNBR] integerValue];
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_trafficPackage2.text = [NSString stringWithFormat:@"%@流量%ldM*%ld",dic[@"operatorName"],flowCount,number];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_trafficPackage2.text =[NSString stringWithFormat:@"%@流量%@G*%ld",dic[@"operatorName"],[formatter stringFromNumber:@(gcount)],number];
            }

        }
        if (self.m_myCartArray.count>=3)
        {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage2.hidden = NO;
            cell.ui_trafficPackage3.hidden = NO;
            NSDictionary * dic = self.m_myCartArray[2];
            NSString * sequenceNBR = dic[@"sequenceNBR"];
            NSInteger number = [[self.m_myCartDictionary objectForKey:sequenceNBR] integerValue];
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_trafficPackage3.text = [NSString stringWithFormat:@"%@流量%ldM*%ld",dic[@"operatorName"],flowCount,number];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_trafficPackage3.text =[NSString stringWithFormat:@"%@流量%@G*%ld",dic[@"operatorName"],[formatter stringFromNumber:@(gcount)],number];
            }

        }
        if (self.m_myCartArray.count>=4)
        {
            cell.ui_points.hidden = NO;
            cell.ui_trafficPackage2.hidden = NO;
            cell.ui_trafficPackage3.hidden = NO;
       }
    }
    else
    {
        NSArray * array = self.toPayDic[@"productList"];
        cell.ui_goodsCount.text = [NSString stringWithFormat:@"查看%ld件商品",array.count];
        if (array.count>=1)
        {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage2.hidden = YES;
            cell.ui_trafficPackage3.hidden = YES;
            NSDictionary * dic = array[0];
            NSInteger number = [dic[@"originalCount"] integerValue];
            NSString * operator = @"";
            if ([dic[@"operatorCode"] isEqualToString:@"10086"])
            {
                operator = @"中国移动";
            }
            else if ([dic[@"operatorCode"] isEqualToString:@"10010"])
            {
                operator = @"中国联通";
            }
            else if ([dic[@"operatorCode"] isEqualToString:@"10000"])
            {
                operator = @"中国电信";
            }
            
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_trafficPackage1.text = [NSString stringWithFormat:@"%@流量%ldM*%ld",operator,flowCount,number];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_trafficPackage1.text =[NSString stringWithFormat:@"%@流量%@G*%ld",operator,[formatter stringFromNumber:@(gcount)],number];
            }
        }
        if (array.count>=2)
        {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage2.hidden = NO;
            cell.ui_trafficPackage3.hidden = YES;
            NSDictionary * dic = array[1];
            NSInteger number = [dic[@"originalCount"] integerValue];
            NSString * operator = @"";
            if ([dic[@"operatorCode"] isEqualToString:@"10086"])
            {
                operator = @"中国移动";
            }
            else if ([dic[@"operatorCode"] isEqualToString:@"10010"])
            {
                operator = @"中国联通";
            }
            else if ([dic[@"operatorCode"] isEqualToString:@"10000"])
            {
                operator = @"中国电信";
            }
            
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_trafficPackage2.text = [NSString stringWithFormat:@"%@流量%ldM*%ld",operator,flowCount,number];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_trafficPackage2.text =[NSString stringWithFormat:@"%@流量%@G*%ld",operator,[formatter stringFromNumber:@(gcount)],number];
            }
        }
        if (array.count>=3)
        {
            cell.ui_points.hidden = YES;
            cell.ui_trafficPackage2.hidden = NO;
            cell.ui_trafficPackage3.hidden = NO;
            NSDictionary * dic = array[2];
            NSInteger number = [dic[@"originalCount"] integerValue];
            NSString * operator = @"";
            if ([dic[@"operatorCode"] isEqualToString:@"10086"])
            {
                operator = @"中国移动";
            }
            else if ([dic[@"operatorCode"] isEqualToString:@"10010"])
            {
                operator = @"中国联通";
            }
            else if ([dic[@"operatorCode"] isEqualToString:@"10000"])
            {
                operator = @"中国电信";
            }
            
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_trafficPackage3.text = [NSString stringWithFormat:@"%@流量%ldM*%ld",operator,flowCount,number];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_trafficPackage3.text =[NSString stringWithFormat:@"%@流量%@G*%ld",operator,[formatter stringFromNumber:@(gcount)],number];
            }
        }
        if (array.count>=4)
        {
            cell.ui_points.hidden = NO;
            cell.ui_trafficPackage2.hidden = NO;
            cell.ui_trafficPackage3.hidden = NO;
        }
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==1)
    {
        if ([self.m_myCartArray isKindOfClass:[NSArray class]])
        {
            XHGoodsViewController * viewController = [[XHGoodsViewController alloc]init];
            viewController.title = @"商品列表";
            viewController.delegate=self;
            viewController.m_myCartArray = self.m_myCartArray;
            viewController.m_myCartDictionary = self.m_myCartDictionary;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else
        {
            XHGoodsViewController * viewController = [[XHGoodsViewController alloc]init];
            viewController.title = @"商品列表";
            viewController.toPayDic = self.toPayDic;
            viewController.toPayArray = self.toPayDic[@"productList"];
            [self.navigationController pushViewController:viewController animated:YES];

        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    if (indexPath.section==0)
    {
        height = 210;
    }
    else if (indexPath.section==1)
    {
        if ([self.m_myCartArray isKindOfClass:[NSArray class]])
        {
            if (self.m_myCartArray.count==1)
            {
                height = 95;
            }
            else if (self.m_myCartArray.count==2)
            {
                height = 125;
            }
            else if (self.m_myCartArray.count==3)
            {
                height = 155;
            }
            else
            {
                height = 175;
            }
        }
        else
        {
            NSArray * array = self.toPayDic[@"productList"];
            if (array.count==1)
            {
                height = 95;
            }
            else if (array.count==2)
            {
                height = 125;
            }
            else if (array.count==3)
            {
                height = 155;
            }
            else
            {
                height = 175;
            }
        }
    }
    return  height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0;
    if (section==0)
    {
        height = 0.1;
    }
    else
    {
        height = 10;
    }
    return height;
}

- (IBAction)buyAction:(id)sender
{
    XHMethodPaymentViewController * viewController = [[XHMethodPaymentViewController alloc]init];
    viewController.modalPresentationStyle = UIModalPresentationCustom;
    viewController.delegate = self;
    viewController.m_integral=self.m_totalMoney;
    [self presentViewController:viewController animated:NO completion:^{
        
    }];
}

#pragma - mark XHMethodPaymentViewDelegate 支付代理

- (void)didSelectedMethodPayment:(NSString *)payMethod
{
    if ([self.m_myCartArray isKindOfClass:[NSArray class]])
    {
        //理财购买支付
        [self buyMoneyTraffic:payMethod];
    }
    else
    {
        //待支付订单
        [self toPayOrder:payMethod];
    }
}
//重新支付理财订单
- (void)toPayOrder:(NSString*)payMethod
{
    if (payMethod == kPayChannelWX)
    {
        WS(weakSelf)
        [PromptAction showWaitPrompt:@"正在支付..."];
        [ServiceManager repayOrderWithOrderSn:self.toPayDic[@"orderSn"] payMethod:kPayChannelWX password:@"" isIntelligent:NO success:^(NSDictionary *data) {
            NSString * charge = [NSString jsonStringWithNSDictionary:data];
            [Pingpp createPayment:charge viewController:weakSelf appURLScheme:appUrlScheme withCompletion:^(NSString *result, PingppError *error) {
                [PromptAction dismissPrompt];
                NSLog(@"completion block: %@", result);
                if (error == nil) {
                    NSLog(@"PingppError is nil");
                } else {
                    NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);
                }
                if ([result isEqualToString:@"success"]) {
                    [ServiceManager payFinishWithChargeId:data[@"id"] success:^(NSDictionary *data) {
                        
                    } failure:^(NSDictionary *data) {
                        
                    }];
                    [weakSelf goPaySuccessViewController];
                }else
                {
                    [weakSelf goPayFailureViewController];
                }
                
            }];
        } failure:^(NSDictionary *data) {
            [PromptAction dismissPrompt];
            [weakSelf goPayFailureViewController];
        }];
        
    }
    else if (payMethod == kPayChannelAlipay)
    {
        WS(weakSelf)
        [ServiceManager repayOrderWithOrderSn:self.toPayDic[@"orderSn"] payMethod:kPayChannelAlipay password:@"" isIntelligent:NO success:^(NSDictionary *data) {
            NSString * charge = [NSString jsonStringWithNSDictionary:data];
            [Pingpp createPayment:charge viewController:weakSelf appURLScheme:appUrlScheme withCompletion:^(NSString *result, PingppError *error) {
                [PromptAction dismissPrompt];
                NSLog(@"completion block: %@", result);
                if (error == nil) {
                    NSLog(@"PingppError is nil");
                } else {
                    NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);
                }
                if ([result isEqualToString:@"success"]) {
                    [ServiceManager payFinishWithChargeId:data[@"id"] success:^(NSDictionary *data) {
                        
                    } failure:^(NSDictionary *data) {
                        
                    }];
                    [weakSelf goPaySuccessViewController];
                }else
                {
                    [weakSelf goPayFailureViewController];
                }

            }];
        } failure:^(NSDictionary *data) {
            [PromptAction dismissPrompt];
            [weakSelf goPayFailureViewController];
        }];
    }
    else if (payMethod == kPayChannelYE)
    {
        //余额
        UIAlertView *alertView=[[UIAlertView alloc] init];
        alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
        alertView.title=@"请输入密码";
        alertView.message = @"登录密码即支付密码";
        UITextField *textField=[alertView textFieldAtIndex:0];
        textField.text=@"";
        textField.textAlignment = NSTextAlignmentCenter;
        [textField setSecureTextEntry:YES];
        RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
        RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"确定" action:^{
            
            if ([NSString checkNullString:textField.text])
            {
                [PromptAction showInfoPrompt:@"请输入密码"];
            }
            else
            {
                WS(weakSelf)
                NSLog(@"%@",textField.text);
                NSLog(@"%@",self.m_myCartArray);
                [PromptAction showWaitPrompt:@"正在支付..."];
                self.ui_tableView.userInteractionEnabled = NO;
                [ServiceManager repayOrderWithOrderSn:self.toPayDic[@"orderSn"] payMethod:kPayChannelYE password:textField.text isIntelligent:NO success:^(NSDictionary *data) {
                    [PromptAction dismissPrompt];
                    [weakSelf goPaySuccessViewController];
                } failure:^(NSDictionary *data) {
                    [PromptAction dismissPrompt];
                    [weakSelf goPayFailureViewController];
                }];
            }
        }];
        [alertView addButtonItem:cancelItem];
        [alertView addButtonItem:okItem];
        [alertView show];
    }
}

- (void)buyMoneyTraffic:(NSString*)payMethod
{
    if (payMethod == kPayChannelWX)
    {
        //微信
        [PromptAction showWaitPrompt:@"正在支付..."];
        self.ui_tableView.userInteractionEnabled = NO;
        [ServiceManager postUserBuyFinancialProductWithPassWord:@"" amount:self.m_totalMoney payMethod:kPayChannelWX market:self.m_myCartDictionary success:^(NSDictionary *data) {
            self.ui_tableView.userInteractionEnabled = YES;
            NSString * charge = [NSString jsonStringWithNSDictionary:data];
            [PromptAction dismissPrompt];
            WS(weakSelf)
            [Pingpp createPayment:charge viewController:weakSelf appURLScheme:appUrlScheme withCompletion:^(NSString *result, PingppError *error) {
                NSLog(@"completion block: %@", result);
                if (error == nil) {
                    NSLog(@"PingppError is nil");
                } else {
                    NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);
                }
                if ([result isEqualToString:@"success"])
                {
                    [ServiceManager payFinishWithChargeId:data[@"id"] success:^(NSDictionary *data) {
                        
                    } failure:^(NSDictionary *data) {
                        
                    }];
                    //支付成功
                    [weakSelf goPaySuccessViewController];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateNotification" object:nil];
                }else
                {
                    //支付失败
                    [weakSelf goPayFailureViewController];
                }
                
            }];
            
        } failure:^(NSDictionary *data) {
            self.ui_tableView.userInteractionEnabled = YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateNotification" object:nil];
        }];
        
    }
    else if (payMethod == kPayChannelAlipay)
    {
        //支付宝
        [PromptAction showWaitPrompt:@"正在支付..."];
        self.ui_tableView.userInteractionEnabled = NO;
        [ServiceManager postUserBuyFinancialProductWithPassWord:@"" amount:self.m_totalMoney payMethod:kPayChannelAlipay market:self.m_myCartDictionary success:^(NSDictionary *data) {
            self.ui_tableView.userInteractionEnabled = YES;
            NSString * charge = [NSString jsonStringWithNSDictionary:data];
            [PromptAction dismissPrompt];
            WS(weakSelf)
            [Pingpp createPayment:charge viewController:weakSelf appURLScheme:appUrlScheme withCompletion:^(NSString *result, PingppError *error) {
                NSLog(@"completion block: %@", result);
                if (error == nil) {
                    NSLog(@"PingppError is nil");
                } else {
                    NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);
                }
                if ([result isEqualToString:@"success"])
                {
                    [ServiceManager payFinishWithChargeId:data[@"id"] success:^(NSDictionary *data) {
                        
                    } failure:^(NSDictionary *data) {
                        
                    }];
                    //支付成功
                    [weakSelf goPaySuccessViewController];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateNotification" object:nil];
                }else
                {
                    //支付失败
                    [weakSelf goPayFailureViewController];
                }
            }];
            
        } failure:^(NSDictionary *data) {
            self.ui_tableView.userInteractionEnabled = YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateNotification" object:nil];
        }];
    }
    else if (payMethod == kPayChannelYE)
    {
        //余额
        UIAlertView *alertView=[[UIAlertView alloc] init];
        alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
        alertView.title=@"请输入密码";
        alertView.message = @"登录密码即支付密码";
        UITextField *textField=[alertView textFieldAtIndex:0];
        textField.text=@"";
        textField.textAlignment = NSTextAlignmentCenter;
        [textField setSecureTextEntry:YES];
        RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
        RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"确定" action:^{
            
            if ([NSString checkNullString:textField.text])
            {
                [PromptAction showInfoPrompt:@"请输入密码"];
            }
            else
            {
                NSLog(@"%@",textField.text);
                NSLog(@"%@",self.m_myCartArray);
                [PromptAction showWaitPrompt:@"正在支付..."];
                self.ui_tableView.userInteractionEnabled = NO;
                [ServiceManager postUserBuyFinancialProductWithPassWord:textField.text amount:self.m_totalMoney payMethod:kPayChannelYE market:self.m_myCartDictionary success:^(NSDictionary *data) {
                    [PromptAction dismissPrompt];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateNotification" object:nil];
                    self.ui_tableView.userInteractionEnabled = YES;
                    [PromptAction showSuccessPrompt:@"支付成功"];
                    [self goPaySuccessViewController];
                } failure:^(NSDictionary *data) {
                    self.ui_tableView.userInteractionEnabled = YES;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateNotification" object:nil];
                }];
            }
        }];
        [alertView addButtonItem:cancelItem];
        [alertView addButtonItem:okItem];
        [alertView show];
    }
}

#pragma - mark 支付成功

- (void)goPaySuccessViewController
{
    XHPaySuccessViewController * viewController = [[XHPaySuccessViewController alloc]init];
    viewController.type = @"money";
    if ([NSString checkNullString:self.m_totalMoney])
    {
        viewController.amount = [self.toPayDic[@"amount"] doubleValue];
    }
    else
    {
        viewController.amount = [self.m_totalMoney doubleValue];
    }
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma - mark 支付失败

- (void)goPayFailureViewController
{
    XHPayFailureViewController * viewController = [[XHPayFailureViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma - mark updateGoodsPrice

- (void)updateGoodsPrice:(NSString*)price ogPrice:(double)ogPrice
{
    self.m_totalMoney = [NSString stringWithFormat:@"%@",price];
    float spreadPrice;
    spreadPrice = ogPrice-[price doubleValue];
    
    [self.ui_buyNow setTitle:[NSString stringWithFormat:@"立即支付%@元(已节省￥%.2f元)",self.m_totalMoney,spreadPrice] forState:UIControlStateNormal];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
